
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/common/assets_utils.dart';
import 'package:fzcgx_app/provider/provider_widget.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/home/page/home_order_page.dart';
import 'package:fzcgx_app/ui/home/view_model/home_view_model.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ProviderWidget<HomeViewModel>(
      model: HomeViewModel(),
      onReady: (viewModel) {
        viewModel.requestApi();
      },
      builder: (context, viewModel, child) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.white,
            elevation: 0.0,
            titleSpacing: 0.0,
            automaticallyImplyLeading: false,
            title: Container(
              child: Row(
                children: <Widget>[
                  SizedBox(
                    width: AdapterUtils.getWidth(15),
                  ),
                  Image.asset(AssetsUtils.getAssetsImagesPath('location.png')),
                  SizedBox(
                    width: AdapterUtils.getWidth(6),
                  ),
                  Text(
                    '杭州',
                    style: TextStyle(
                        color: Color(0xff141F33),
                        fontSize: AdapterUtils.getSp(14),
                        fontWeight: FontWeight.w600),
                  ),
                  SizedBox(
                    width: AdapterUtils.getWidth(10),
                  ),
                  Expanded(
                    child: ConstrainedBox(
                      constraints: BoxConstraints(
                        maxHeight: AdapterUtils.getHeight(40),
                      ),
                      child: TextField(
                        style: TextStyle(
                          color: Color(0xff3B4859),
                          fontSize: AdapterUtils.getSp(12),
                          fontWeight: FontWeight.w500,
                        ),
                        decoration: InputDecoration(
                          contentPadding:
                              const EdgeInsets.symmetric(vertical: 4.0),
                          hintText: '搜索你要寻找的信息',
                          hintStyle: TextStyle(
                            color: Color(0xff99A2AD),
                            fontSize: AdapterUtils.getSp(12),
                            fontWeight: FontWeight.w500,
                          ),
                          prefixIcon: Image.asset(
                              AssetsUtils.getAssetsImagesPath('search.png')),
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(20),
                              borderSide: BorderSide.none),
                          filled: true,
                          fillColor: Color(0xffF3F3F3),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    width: AdapterUtils.getWidth(15),
                  ),
                ],
              ),
            ),
            bottom: PreferredSize(
                preferredSize: Size(
                    AdapterUtils.screenWidth(), AdapterUtils.getHeight(50)),
                child: Expanded(
                  child: Container(
                    margin: EdgeInsets.symmetric(
                        horizontal: AdapterUtils.getWidth(15)),
                    child: ListView.builder(
                        scrollDirection: Axis.horizontal,
                        itemCount: viewModel.titles.length,
                        itemBuilder: (BuildContext context, int index) {
                          return InkWell(
                            onTap: () {
                              viewModel.updateIndex(index);
                            },
                            child: Container(
                              margin: EdgeInsets.only(
                                  right: AdapterUtils.getWidth(22)),
                              alignment: Alignment.center,
                              child: Text(
                                viewModel.titles[index],
                                style: viewModel.currentIndex == index
                                    ? TextStyle(
                                        color: Color(0xff141F33),
                                        fontSize: AdapterUtils.getSp(24),
                                        fontWeight: FontWeight.bold)
                                    : TextStyle(
                                        color: Color(0xff606B79),
                                        fontSize: AdapterUtils.getSp(16),
                                        fontWeight: FontWeight.w500),
                              ),
                            ),
                          );
                        }),
                  ),
                )),
          ),
          body: viewModel.state == ViewState.Success
              ? PageView(
                  controller: viewModel.pageController,
                  onPageChanged: (index) {
                    viewModel.updateIndex(index);
                  },
                  children: [
                    HomeOrderPage(
                      viewModel: viewModel,
                    ),
                    HomeOrderPage(),
                    HomeOrderPage(),
                    HomeOrderPage()
                  ],
                )
              : Container(),
          floatingActionButton: FloatingActionButton(
            backgroundColor: Colors.transparent,
            isExtended: true,
            elevation: 0,
            child: Stack(
              children: <Widget>[
                AspectRatio(
                  aspectRatio: 1.0,
                  child: Image.asset(
                    AssetsUtils.getAssetsImagesPath('oval_btn.png'),
                    fit: BoxFit.cover,
                  ),
                ),
                Center(
                  child: Text(
                    '发布',
                    style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.w500,
                        fontSize: AdapterUtils.getSp(12)),
                  ),
                )
              ],
            ),
            onPressed: () {
             viewModel.showMenu(context);
            },
          ),
        );
      },
    );
  }

  
}
